package tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName leftLeafBottom
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-14 19:59
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class leftLeafBottom {
    /**
     * 513.找树左下角的值
     * 力扣题目链接(opens new window)
     * <p>
     * 给定一个二叉树，在树的最后一行找到最左边的值。
     */

    public int findBottomLeftValue(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);

        int res = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (i == 0) {
                    res = poll.val;
                }

                if (poll.left != null) queue.add(poll.left);
                if (poll.right != null) queue.add(poll.right);
            }
        }

        return res;
    }


    /**
     * 513.找树左下角的值
     * 力扣题目链接(opens new window)
     * <p>
     * 给定一个二叉树，在树的最后一行找到最左边的值。
     */

    public int findBottomLeftValue2(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int res = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (i == 0) {
                    res = poll.val;
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(2);

        TreeNode treeNode1 = new TreeNode(3);


        TreeNode treeNode2 = new TreeNode(4);


//        treeNode.left = treeNode1;
        treeNode.right = treeNode2;
        TreeNode treeNode3 = new TreeNode(55);

        treeNode1.left = treeNode3;

        leftLeafBottom leftLeafBottom = new leftLeafBottom();
        int bottomLeftValue = leftLeafBottom.findBottomLeftValue(treeNode);
        System.out.println(bottomLeftValue);

    }
}